<?php
namespace TYPO3\CMS\Fluid\ViewHelpers\Widget\Controller;

/*                                                                        *
 * This script is backported from the TYPO3 Flow package "TYPO3.Fluid".   *
 *                                                                        *
 * It is free software; you can redistribute it and/or modify it under    *
 * the terms of the GNU Lesser General Public License, either version 3   *
 *  of the License, or (at your option) any later version.                *
 *                                                                        *
 *                                                                        *
 * This script is distributed in the hope that it will be useful, but     *
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN-    *
 * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser       *
 * General Public License for more details.                               *
 *                                                                        *
 * You should have received a copy of the GNU Lesser General Public       *
 * License along with the script.                                         *
 * If not, see http://www.gnu.org/licenses/lgpl.html                      *
 *                                                                        *
 * The TYPO3 project - inspiring people to share!                         *
 *                                                                        */
use TYPO3\CMS\Core\Utility\ArrayUtility;
use TYPO3\CMS\Extbase\Persistence\ObjectStorage;
use TYPO3\CMS\Extbase\Persistence\QueryResultInterface;
use TYPO3\CMS\Fluid\Core\Widget\AbstractWidgetController;

/**
 * Class PaginateController
 */
class PaginateController extends AbstractWidgetController {

	/**
	 * @var array
	 */
	protected $configuration = array(
		'itemsPerPage' => 10,
		'insertAbove' => FALSE,
		'insertBelow' => TRUE,
		'maximumNumberOfLinks' => 99,
		'addQueryStringMethod' => ''
	);

	/**
	 * @var QueryResultInterface|ObjectStorage|array
	 */
	protected $objects;

	/**
	 * @var int
	 */
	protected $currentPage = 1;

	/**
	 * @var int
	 */
	protected $maximumNumberOfLinks = 99;

	/**
	 * @var int
	 */
	protected $numberOfPages = 1;

	/**
	 * @var int
	 */
	protected $displayRangeStart = NULL;

	/**
	 * @var int
	 */
	protected $displayRangeEnd = NULL;

	/**
	 * @return void
	 */
	public function initializeAction() {
		$this->objects = $this->widgetConfiguration['objects'];
		ArrayUtility::mergeRecursiveWithOverrule($this->configuration, $this->widgetConfiguration['configuration'], FALSE);
		$this->numberOfPages = ceil(count($this->objects) / (int)$this->configuration['itemsPerPage']);
		$this->maximumNumberOfLinks = (int)$this->configuration['maximumNumberOfLinks'];
	}

	/**
	 * @param int $currentPage
	 * @return void
	 */
	public function indexAction($currentPage = 1) {
		// set current page
		$this->currentPage = (int)$currentPage;
		if ($this->currentPage < 1) {
			$this->currentPage = 1;
		}
		if ($this->currentPage > $this->numberOfPages) {
			// set $modifiedObjects to NULL if the page does not exist
			$modifiedObjects = NULL;
		} else {
			// modify query
			$itemsPerPage = (int)$this->configuration['itemsPerPage'];
			$offset = 0;
			if ($this->currentPage > 1) {
				$offset = ((int)($itemsPerPage * ($this->currentPage - 1)));
			}
			$modifiedObjects = $this->prepareObjectsSlice($itemsPerPage, $offset);
		}
		$this->view->assign('contentArguments', array(
			$this->widgetConfiguration['as'] => $modifiedObjects
		));
		$this->view->assign('configuration', $this->configuration);
		$this->view->assign('pagination', $this->buildPagination());
	}

	/**
	 * If a certain number of links should be displayed, adjust before and after
	 * amounts accordingly.
	 *
	 * @return void
	 */
	protected function calculateDisplayRange() {
		$maximumNumberOfLinks = $this->maximumNumberOfLinks;
		if ($maximumNumberOfLinks > $this->numberOfPages) {
			$maximumNumberOfLinks = $this->numberOfPages;
		}
		$delta = floor($maximumNumberOfLinks / 2);
		$this->displayRangeStart = $this->currentPage - $delta;
		$this->displayRangeEnd = $this->currentPage + $delta - ($maximumNumberOfLinks % 2 === 0 ? 1 : 0);
		if ($this->displayRangeStart < 1) {
			$this->displayRangeEnd -= $this->displayRangeStart - 1;
		}
		if ($this->displayRangeEnd > $this->numberOfPages) {
			$this->displayRangeStart -= $this->displayRangeEnd - $this->numberOfPages;
		}
		$this->displayRangeStart = (int)max($this->displayRangeStart, 1);
		$this->displayRangeEnd = (int)min($this->displayRangeEnd, $this->numberOfPages);
	}

	/**
	 * Returns an array with the keys "pages", "current", "numberOfPages",
	 * "nextPage" & "previousPage"
	 *
	 * @return array
	 */
	protected function buildPagination() {
		$this->calculateDisplayRange();
		$pages = array();
		for ($i = $this->displayRangeStart; $i <= $this->displayRangeEnd; $i++) {
			$pages[] = array('number' => $i, 'isCurrent' => $i === $this->currentPage);
		}
		$pagination = array(
			'pages' => $pages,
			'current' => $this->currentPage,
			'numberOfPages' => $this->numberOfPages,
			'displayRangeStart' => $this->displayRangeStart,
			'displayRangeEnd' => $this->displayRangeEnd,
			'hasLessPages' => $this->displayRangeStart > 2,
			'hasMorePages' => $this->displayRangeEnd + 1 < $this->numberOfPages
		);
		if ($this->currentPage < $this->numberOfPages) {
			$pagination['nextPage'] = $this->currentPage + 1;
		}
		if ($this->currentPage > 1) {
			$pagination['previousPage'] = $this->currentPage - 1;
		}
		return $pagination;
	}

	/**
	 * @param int $itemsPerPage
	 * @param int $offset
	 *
	 * @return array|QueryResultInterface
	 * @throws \InvalidArgumentException
	 */
	protected function prepareObjectsSlice($itemsPerPage, $offset) {
		if ($this->objects instanceof QueryResultInterface) {
			$query = $this->objects->getQuery();
			$query->setLimit($itemsPerPage);
			if ($offset > 0) {
				$query->setOffset($offset);
			}
			$modifiedObjects = $query->execute();
			return $modifiedObjects;
		} elseif ($this->objects instanceof ObjectStorage) {
			$modifiedObjects = array();
			$endOfRange = $offset + $itemsPerPage;
			for ($i = $offset; $i < $endOfRange; $i++) {
				$modifiedObjects[] = $this->objects->toArray()[$i];
			}
			return $modifiedObjects;
		} elseif (is_array($this->objects)) {
			$modifiedObjects = array_slice($this->objects, $offset, $itemsPerPage);
			return $modifiedObjects;
		} else {
			throw new \InvalidArgumentException('The view helper "' . get_class($this)
				. '" accepts as argument "QueryResultInterface", "\SplObjectStorage", "ObjectStorage" or an array. '
				. 'given: ' . get_class($this->objects), 1385547291
			);
		}
	}

}
